package com.xmtlxx.wms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.wms.domain.ViewingDtl;
import com.xmtlxx.wms.domain.bo.ViewingDtlBo;
import com.xmtlxx.wms.domain.vo.ViewingDtlVo;
import com.xmtlxx.wms.mapper.ViewingDtlMapper;
import com.xmtlxx.wms.service.IViewingDtlService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmtlxx.system.domain.bo.SysOssBo;
import com.xmtlxx.system.mapper.SysOssMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 看样明细Service业务层处理
 *
 * @author xmtlxx
 * @date 2022-08-15
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class ViewingDtlServiceImpl implements IViewingDtlService {

    private final ViewingDtlMapper baseMapper;

    private final SysOssMapper sysOssMapper;

    /**
     * 查询看样明细
     *
     * @param id 看样明细主键
     * @return 看样明细
     */
    @Override
    public ViewingDtlVo queryById(String id){
        //return baseMapper.selectVoById(id);
        return baseMapper.selectViewingDtlVoByID(id);
    }

    /**
     * 查询看样明细列表
     *
     * @param bo 看样明细
     * @return 看样明细
     */
    @Override
    public TableDataInfo<ViewingDtlVo> queryPageList(ViewingDtlBo bo, PageQuery pageQuery) {
       // LambdaQueryWrapper<ViewingDtl> lqw = buildQueryWrapper(bo);
       // Page<ViewingDtlVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<ViewingDtlVo> result = baseMapper.selectPageViewingDtlList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询看样明细列表
     *
     * @param bo 看样明细
     * @return 看样明细
     */
    @Override
    public List<ViewingDtlVo> queryList(ViewingDtlBo bo) {
       // LambdaQueryWrapper<ViewingDtl> lqw = buildQueryWrapper(bo);
       // return baseMapper.selectVoList(lqw);
        List<ViewingDtlVo> resutls = baseMapper.selectViewingDtlList(bo);
        if(resutls != null && resutls.size() > 0){
            for (ViewingDtlVo item:resutls){
                SysOssBo ossBo = new SysOssBo();
                ossBo.setBuss("property");
                ossBo.setObjid(item.getPno());
                item.setOss(sysOssMapper.selectSysOssList(ossBo));
            }
        }
        return resutls;
    }

    private LambdaQueryWrapper<ViewingDtl> buildQueryWrapper(ViewingDtlBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ViewingDtl> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getViewingId()), ViewingDtl::getViewingId, bo.getViewingId());
        lqw.eq(StringUtils.isNotBlank(bo.getPno()), ViewingDtl::getPno, bo.getPno());
        lqw.like(StringUtils.isNotBlank(bo.getName()), ViewingDtl::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), ViewingDtl::getStatus, bo.getStatus());
        lqw.between(params.get("beginViewTime") != null && params.get("endViewTime") != null,
            ViewingDtl::getViewTime ,params.get("beginViewTime"), params.get("endViewTime"));
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), ViewingDtl::getDelFlag, bo.getDelFlag());
        return lqw;
    }

    /**
     * 新增看样明细
     *
     * @param bo 看样明细
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(ViewingDtlBo bo) {
        ViewingDtl add = BeanUtil.toBean(bo, ViewingDtl.class);
        validEntityBeforeSave(add);

        //填充UUID
        add.setId(IdUtil.simpleUUID());
        add.setDelFlag("0");
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改看样明细
     *
     * @param bo 看样明细
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(ViewingDtlBo bo) {
        ViewingDtl update = BeanUtil.toBean(bo, ViewingDtl.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ViewingDtl entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除看样明细
     *
     * @param ids 需要删除的看样明细主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
